﻿require 'Qt4'
load 'ui_todayform.rb'


class TaskViewer < Qt::MainWindow
	attr_accessor :ui
	attr_accessor :db
	attr_accessor :see_all
	attr_accessor :slice
	slots 'spentTimeChanged(QTime)','taskNoticeChanged()','toggleSeeAll()'
	slots 'markAsCompleted()','markAsNonCompleted()','selectedTaskChanged(int,int,int,int)'
	slots 'currentDateChanged(QDate)'
	
	def init(db)
		@ui=Ui_TodayForm.new
        @ui.setupUi(self)
		
		@ui.dateCurrentDate.setDate(DTConversions::dateToQDate(Date.today))
		
		@db=db
		@see_all=false
		
		Qt::Object.connect(@ui.tblData,SIGNAL('currentCellChanged(int,int,int,int)'),self,SLOT('selectedTaskChanged(int,int,int,int)'))
		Qt::Object.connect(@ui.txtNotice,SIGNAL('textChanged()'),self,SLOT('taskNoticeChanged()'))
		Qt::Object.connect(@ui.timeWasted,SIGNAL('timeChanged(QTime)'),self,SLOT('spentTimeChanged(QTime)'))
		Qt::Object.connect(@ui.btnViewCompleted,SIGNAL('clicked()'),self,SLOT('toggleSeeAll()'))
		Qt::Object.connect(@ui.btnMarkAsNonCompleted,SIGNAL('clicked()'),self,SLOT('markAsNonCompleted()'))
		Qt::Object.connect(@ui.btnMarkAsCompleted,SIGNAL('clicked()'),self,SLOT('markAsCompleted()'))
		Qt::Object.connect(@ui.dateCurrentDate,SIGNAL('dateChanged(QDate)'),self,SLOT('currentDateChanged(QDate)'))
		
		@ui.tblData.setColumnCount(4)
		@ui.tblData.setHorizontalHeaderLabels(['Имя','Время','Потрачено','Статус'])
		@ui.tblData.horizontalHeader().resizeSection(0,375);
		@ui.tblData.horizontalHeader().resizeSection(1,65);
	    @ui.tblData.horizontalHeader().resizeSection(2,65);
		@ui.tblData.horizontalHeader().resizeSection(3,80);
		
		@slice=@db.getDatabaseTableSlice(Date.today,@see_all)
		@slice.enumerateTable(self)
		self.show()
	end
	
	
	def deleteRow(index)
		@ui.tblData.removeRow(index)
	end
	
	def insertTask(row,task)
		@ui.tblData.insertRow(row)

		@ui.tblData.setItem(row,0,Qt::TableWidgetItem.new(task.getName()))				
		@ui.tblData.setItem(row,1,Qt::TableWidgetItem.new(task.getExecutionTime().toString()))		
		@ui.tblData.setItem(row,2,Qt::TableWidgetItem.new(task.getSpentTime().toString()))
		statusString="Не выполнено";
		if (task.getStatus()==false)
			statusString="Выполнено";
		end
		@ui.tblData.setItem(row,3,Qt::TableWidgetItem.new(statusString))
	end
	
	def updateTaskStatus(row,new_status)
		statusString="Не выполнено";
		if (new_status==false)
			statusString="Выполнено";
		end
		@ui.tblData.setItem(row,3,Qt::TableWidgetItem.new(statusString))
	end
	
	def insertTarget(row,target)
	    @ui.tblData.insertRow(row)
		
		@ui.tblData.setItem(row,0,Qt::TableWidgetItem.new("#" + target.getName()))	
		statusString="Не выполнено";
		if (target.getStatus()==false)
			statusString="Выполнено";
		end
		@ui.tblData.setItem(row,3,Qt::TableWidgetItem.new(statusString))
	end
	
	def spentTimeChanged(time)
		row = @ui.tblData.currentRow()
		task = @slice.findTaskByRow(row)
		if (task!=nil)
			task.setSpentTime(@ui.timeWasted.time())
			@ui.tblData.setItem(row,2,Qt::TableWidgetItem.new(task.getSpentTime().toString()))
		end
	end
	
	def taskNoticeChanged()
		row = @ui.tblData.currentRow()
		task = @slice.findTaskByRow(row)
		if (task!=nil)
			task.setNotice(@ui.txtNotice.plainText())
		end
	end

	def selectedTaskChanged(currentRow,currentColumn,previousRow,previousColumn)
		task = @slice.findTaskByRow(currentRow)
		if (task!=nil)
			@ui.timeWasted.setTime(task.getSpentTime())
			@ui.txtNotice.setPlainText(task.getNotice())
		end
	end

	
	def toggleSeeAll()
		@ui.tblData.clearContents()
		@ui.tblData.setRowCount(0)
		@see_all = (not @see_all)
		
		newText = "Только невыполненные"
		if (@see_all==false)
			newText = "Посмотреть выполненные"
		end
		@ui.btnViewCompleted.setText(newText)
		@slice=@db.getDatabaseTableSlice(Date.today,@see_all)
		@slice.enumerateTable(self)
	end
	
	def markAsCompleted()
	   @slice.changeTaskStatus(self,@ui.tblData.currentRow(),@see_all,false)
	end
	
	def markAsNonCompleted()
		@slice.changeTaskStatus(self,@ui.tblData.currentRow(),@see_all,true)
	end
	
	def currentDateChanged(new_current_date)
		row = @ui.tblData.currentRow()
		@ui.tblData.clearContents()
		@ui.tblData.setRowCount(0)
		
		@slice=@db.getDatabaseTableSlice(DTConversions::qdateToDate(new_current_date),@see_all)
		@slice.enumerateTable(self)
	
		if row < @ui.tblData.rowCount()
			@ui.tblData.setCurrentCell(row,0)
		end
	end
	
end